package com.tushen;

import com.tushen.task.Dye3;
import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.Driver;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconstConstants;
import org.gdal.ogr.DataSource;
//import org.gdal.ogr.Driver;

import org.gdal.ogr.ogr;

/**
 * create by likun on 2020/7/14 0014
 */
public class Main {
    public static void main(String[] args) {
        //此处换成自己文件的位置的路径
//        readGDAL("E:\\xxxxxxx.tif");
//        getElevationFromTif("D:\\其他\\核素\\uv_result\\nwp_cur_2019052212.tif");
//        readGDAL("D:\\其他\\核素\\uv_result\\nwp_cur_2019052212.tif");

//        readGDAL("D:\\其他\\核素\\polcon202004210900.tif");
////        shpToGeojson("D:\\公交地铁静态数据\\北京公交网站点_btismart.shp","D:\\公交地铁静态数据\\北京公交网站点_btismart.geojson");
//        Band band =null;
//        band.SetNoDataValue(0);
        if(args.length<2){
            System.out.println("please input path and verson");
            return;
        }
//        Dye3.executePyToInterPolate(args[0]);
        Dye3.dumpTif(args[0],args[1]);//测试插入tif文件到dye_tif
    }
    /**
     * 读取tif文件的相关信息
     * @param fileName
     */
    public static void readGDAL(String fileName) {
        gdal.AllRegister();
        //读取影像数据
        Dataset dataset = gdal.Open(fileName,gdalconstConstants.GA_ReadOnly);
        if(dataset == null){
            System.err.println("GDALOpen failed - "+gdal.GetLastErrorNo());
            System.err.println(gdal.GetLastErrorMsg());
            System.exit(1);
        }

        Driver driver = dataset.GetDriver();
        System.out.println("Driver:"+driver.getShortName()+"/"+driver.getLongName());

        //读取影像信息
        int xSize = dataset.getRasterXSize();
        int ySize = dataset.getRasterYSize();
        int bandCount = dataset.GetRasterCount();
        System.out.println("size is "+xSize+","+ySize+",光带数："+bandCount);

        Band band = dataset.GetRasterBand(1);
        band.SetNoDataValue(0);
        int type = band.GetRasterDataType();
        //type为1，代表的是Eight bit unsigned integer
        System.out.println(type);

        dataset.delete();
        gdal.GDALDestroyDriverManager();
    }

    /**
     * shp转换geojson
     * @param shpFileName
     * @param geojsonFileName
     */
    public static void shpToGeojson(String shpFileName,String geojsonFileName) {
        // 注册所有的驱动
        ogr.RegisterAll();
        // 为了支持中文路径，请添加下面这句代码
        gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","YES");
        // 为了使属性表字段支持中文，请添加下面这句
        gdal.SetConfigOption("SHAPE_ENCODING","");
        //shp文件所在的位置
        //String strVectorFile = "D:\\sichuan\\sichuanPointALL.shp";
        //打开数据
        DataSource ds = ogr.Open(shpFileName,0);
        if (ds == null)
        {
            System.out.println("打开文件失败！" );
            return;
        }
        System.out.println("打开文件成功！" );
        org.gdal.ogr.Driver dv = ogr.GetDriverByName("GeoJSON");
        if (dv == null)
        {
            System.out.println("打开驱动失败！" );
            return;
        }
        System.out.println("打开驱动成功！" );
        //输出geojson的位置及文件名
        //String geojsonFileName = "D:\\data\\sichuan.geojson";
        dv.CopyDataSource(ds, geojsonFileName);
        System.out.println("转换成功！");
    }
    /**
     * 获取每个点的高程值
     * @param fileName_tif
     */
    public static void getElevationFromTif(String fileName_tif) {
        gdal.AllRegister();
        Dataset hDataset = gdal.Open(fileName_tif, gdalconstConstants.GA_ReadOnly);
        if (hDataset == null)
        {
            System.err.println("GDALOpen failed - " + gdal.GetLastErrorNo());
            System.err.println(gdal.GetLastErrorMsg());

            System.exit(1);
        }

        Driver hDriver = hDataset.GetDriver();
        System.out.println("Driver: " + hDriver.getShortName() + "/" + hDriver.getLongName());
        int iXSize = hDataset.getRasterXSize();
        int iYSize = hDataset.getRasterYSize();
        System.out.println("Size is " + iXSize + ", " + iYSize);

        Band band = hDataset.GetRasterBand(1);
        //这里是DEM数据，所以声明一个int数组来存储，如果是其他数据类型，声明相应的类型即可
        int buf[] = new int[iXSize];

        for(int i=0; i<50/*iYSize*/; i++)
        {
            band.ReadRaster(0, i, iXSize, 1, buf);	//读取一行数据

            // 下面是输出像元值，为了方便，我只输出了左上角 10×10的范围内的数据
            for(int j=0; j<50/*iXSize*/; j++){
                System.out.print(buf[j] + ", ");
            }

            System.out.println("\n");
        }
        hDataset.delete();

        // 可选
        gdal.GDALDestroyDriverManager();
    }

}
